package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.top10.entity.CollectEntity;
import org.top10.entity.Information;
import org.top10.entity.News;
import org.top10.entity.Reading;
import org.top10.entity.Video;
import org.top10.executor.ParseSource;
import org.top10.util.NdcFunction;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class BaiduTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_BAIDU;// 对应网站ID
	private static final String ENCoding =TOP10Global.Encoding_GDK;// 对应网站ID
	
	public BaiduTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new BaiduMoiveParser());
		PARSER.add(new BaiduTVParser());
		PARSER.add(new BaiduRVSHOWParser());
		PARSER.add(new BaiduCartoonParser());
		PARSER.add(new BaiduBookParser());
		PARSER.add(new BaiduSearchParser());
	}
	public static void main(String arg[]) throws Exception{
		new BaiduSearchParser().dealText(ENCoding);
		
	}
	
}
abstract class BaiduParser extends TOPParserImpl {
	
	private static final String URL="http://top.baidu.com/category/yule.html";
	private static final String BaseUrl="http://top.baidu.com/";
	public BaiduParser(String t,int type) {
		super(t,type, URL);
	}
	public abstract List<CollectEntity> parse(ParseSource doc) throws Exception ;

	public abstract CollectEntity build(Node node, int rank) throws Exception ;
		// TODO Auto-generated method stub
	public  List parse1(ParseSource doc,String sign) throws Exception{
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node = getNodeByText(body, "h1",sign).getParentNode();
		ArrayList<Node> trs = new ArrayList<Node>();
		getAllTagsByParent(node, "tr", trs);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		for (int i = 0; i < 10; i++) {
			Node tr=trs.get(i);
			CollectEntity v =build(tr,i+1);
			System.out.println(v);
			list.add(v);
		}
		return list;
	}

	public  void build1(Node node, int rank,CollectEntity v) throws Exception{
		v.setRank(rank);
		List<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		v.setName(as.get(0).getTextContent());
		String url=getAttrValueByTag(as.get(1),"href");
		v.setLinkUrl(BaseUrl+NdcFunction.delPoint2Url(url));
	}
	
}
class BaiduMoiveParser extends BaiduParser{

	private static final String TITLE="电影排行";
	private static final String SIGN="今日电影排行榜";
	public BaiduMoiveParser() {
		super(TITLE, TOP10Global.TYPE_Television);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		Video v = new Video();
		 build1(node,rank,v);
		 return v;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return parse1(doc,SIGN);
	}
	
}
class BaiduTVParser extends BaiduParser{

	private static final String TITLE="电视剧排行";
	private static final String SIGN="今日电视剧排行榜";
	public BaiduTVParser() {
		super(TITLE, TOP10Global.TYPE_Television);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		Video v = new Video();
		 build1(node,rank,v);
		 return v;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return parse1(doc,SIGN);
	}
	
}

class BaiduRVSHOWParser extends BaiduParser{

	private static final String TITLE="娱乐排行";
	private static final String SIGN="今日综艺排行榜";
	public BaiduRVSHOWParser() {
		super(TITLE, TOP10Global.TYPE_Television);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		Video v = new Video();
		 build1(node,rank,v);
		 return v;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return parse1(doc,SIGN);
	}
	
}

class BaiduCartoonParser extends BaiduParser{

	private static final String TITLE="动漫排行";
	private static final String SIGN="今日动漫排行榜";
	public BaiduCartoonParser() {
		super(TITLE, TOP10Global.TYPE_Television);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		Video v = new Video();
		 build1(node,rank,v);
		 return v;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return parse1(doc,SIGN);
	}
	
}

class BaiduBookParser extends BaiduParser{

	private static final String TITLE="小说排行";
	private static final String SIGN="今日小说排行榜";
	public BaiduBookParser() {
		super(TITLE, TOP10Global.TYPE_Reading);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		 Reading v = new Reading();
		 build1(node,rank,v);
		 return v;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		return parse1(doc,SIGN);
	}
	
}

class BaiduSearchParser extends TOPParserImpl{

	private static final String TITLE="热点排行";
	private static final String SIGN="实时热点排行榜";
	private static final String URL="http://top.baidu.com/buzz/top10.html";
	public BaiduSearchParser() {
		super(TITLE, TOP10Global.TYPE_Information,URL);
	}

	public CollectEntity build(Node node, int rank) throws Exception {
		 String url=getAttrValueByTag(node,"href");
		 if(url!=null&&url.indexOf("http://www.baidu.com/baidu")!=-1){
			 Information v = new Information();
			 v.setName(node.getTextContent());
			 v.setRank(rank);
			 v.setLinkUrl(url);
			 return v;
		 }
		return null;
	}

	public List<CollectEntity> parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("body").item(0);
		Node node = getNodeByText(body, "h1",SIGN).getParentNode();
		ArrayList<Node> as = new ArrayList<Node>();
		getAllTagsByParent(node, "a", as);
		List<CollectEntity> list = new ArrayList<CollectEntity>();
		int tank=1;
		for (int i = 0; i < as.size(); i++) {
			CollectEntity v =build(as.get(i),tank);
			if(v!=null){
				System.out.println(v);
				list.add(v);
				tank++;
				if(tank>=11)break;
			}
			
		}
		return list;
	}
	
}
